<template>
  <div >
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/role/list' }">角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑角色</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="edit-role">
      <h3>编辑用户</h3>
      <el-form :model="form" :rules="rules" ref="editRole" label-width="80px">
        <el-form-item label="英文名称"  prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="中文名称"  prop="display_name">
          <el-input v-model="form.display_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述"  >
          <el-input v-model="form.description" type="textarea" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="update">更新</el-button>
          <el-button @click="reset">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "RoleEdit",
    data(){
      return {
        form:{},
        id:this.$route.params.id,
        rules:{
          name:[
            { required: true, message: '请输入昵称', trigger: 'blur' },
            { min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: 'blur' }
          ],
          display_name:[
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
          ]
        },
      }
    },
    created(){
      this.getRole()
    },
    methods:{
      update(){
        this.$refs.editRole.validate(valid =>{
          if(valid){
            this.axios.put(`/api/admin/role/${this.id}`,this.form).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              //this.form = {status:1}; // 清空表单数据
              // this.$refs.addUser.resetFields()
            }).catch(err => {
              this.$message.error(err.response.data.status)
            });
          }
          else{
            this.$message.error("请填写完整信息")
          }
        });
      },
      getRole(){
        this.axios.get(`/api/admin/role/${this.id}`).then(res=>{
          // console.log(res)
          this.form = res.data.data;
        }).catch(err=>{
          this.$message.error(err.response.data.message)
        });
      },
      reset(){
        this.$router.push('/admin/role/list')
      }
    }
  }
</script>

<style scoped lang="less">
  .edit-role{
    width: 400px;
    margin-top: 20px;
  }
</style>
